
package com.coolgor.coldot.service.wechat.gzh.impl;

import com.coolgor.coldot.common.PageCalculator;
import com.coolgor.coldot.entity.wechat.gzh.GZHMessageTemplate;
import com.coolgor.coldot.dao.wechat.gzh.GZHMessageTemplateDao;
import com.coolgor.coldot.dto.wechat.gzh.GZHMessageTemplateExecution;
import com.coolgor.coldot.enums.wechat.gzh.GZHMessageTemplateOperationStateEnum;
import com.coolgor.coldot.exceptions.wechat.gzh.GZHMessageTemplateOperationException;
import com.coolgor.coldot.service.wechat.gzh.GZHMessageTemplateService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;


@Service
public class GZHMessageTemplateServiceImpl implements GZHMessageTemplateService {

	private static Logger log = LoggerFactory.getLogger(GZHMessageTemplateServiceImpl.class);

	@Autowired
	GZHMessageTemplateDao gZHMessageTemplateDao;

	@Override
	public GZHMessageTemplateExecution getByID(Long recordID) {
		// 空值判断
		if (null == recordID || recordID.equals("")) {
			return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.NULL_ID);
		}
		GZHMessageTemplate gZHMessageTemplate = gZHMessageTemplateDao.queryByID(recordID);
		if (gZHMessageTemplate != null) {
			return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.SUCCESS, gZHMessageTemplate);
		} else {
			return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.NULL);
		}
	}

	@Override
	public String getTemplateID(String appID, String templateName) {
		if (null==appID || "".equals(appID)) return null;
		if (null==templateName|| "".equals(templateName)) return null;
		GZHMessageTemplate condition = new GZHMessageTemplate();
		condition.setAppID(appID);
		condition.setName(templateName);
		condition.setFlag(1);
		List<GZHMessageTemplate> list = gZHMessageTemplateDao.queryList(condition,0,1);
		if (null!=list && list.size()==1) {
			GZHMessageTemplate msgTemplate = list.get(0);
			if (null!= msgTemplate){
				return msgTemplate.getTemplateID();
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	@Override
	public GZHMessageTemplateExecution getList(GZHMessageTemplate gZHMessageTemplateCondition, int pageIndex, int pageSize) {
		// 将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		// 依据查询条件，调用dao层返回相关的查询结果列表
		List<GZHMessageTemplate> list = gZHMessageTemplateDao.queryList(gZHMessageTemplateCondition, rowIndex, pageSize);
		// 依据相同的查询条件，返回查询结果总数
		int count = gZHMessageTemplateDao.queryCount(gZHMessageTemplateCondition);
		GZHMessageTemplateExecution result = new GZHMessageTemplateExecution();
		if (list != null) {
			result.setState(GZHMessageTemplateOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(GZHMessageTemplateOperationStateEnum.SUCCESS.getStateInfo());
			result.setGZHMessageTemplateList(list);
			result.setCount(count);
		} else {
			result.setState(GZHMessageTemplateOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public GZHMessageTemplateExecution add(GZHMessageTemplate gZHMessageTemplate) throws GZHMessageTemplateOperationException {
		// 空值判断
		if (gZHMessageTemplate == null) {
			return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			gZHMessageTemplate.setCreateTime(new Date());
			gZHMessageTemplate.setLastEditTime(new Date());
			// 执行插入数据操作 
			effectedNum = gZHMessageTemplateDao.insert(gZHMessageTemplate);
			if (effectedNum <= 0) {
				throw new GZHMessageTemplateOperationException("GZHMessageTemplate信息插入失败");
			}
		} catch (Exception e) {
			throw new GZHMessageTemplateOperationException("Add gZHMessageTemplate error:" + e.getMessage());
		}
		return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.SUCCESS, gZHMessageTemplate);
	}

	@Override
	@Transactional
	public GZHMessageTemplateExecution modify(GZHMessageTemplate gZHMessageTemplate) throws GZHMessageTemplateOperationException {
		// 空值判断
		if (gZHMessageTemplate == null) {
			return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 把记录的最后更新时间设置为当前时间 
			gZHMessageTemplate.setLastEditTime(new Date());
			// 执行修改更新操作 
			effectedNum = gZHMessageTemplateDao.update(gZHMessageTemplate);
			if (effectedNum <= 0) {
				throw new GZHMessageTemplateOperationException("GZHMessageTemplate信息更新失败");
			}
		} catch (Exception e) {
			throw new GZHMessageTemplateOperationException("Update gZHMessageTemplate error:" + e.getMessage());
		}
		return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.SUCCESS, gZHMessageTemplate);
	}

	@Override
	@Transactional
	public GZHMessageTemplateExecution remove(Long recordID) throws GZHMessageTemplateOperationException{
		// 空值判断
		if (recordID == null) {
			return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = gZHMessageTemplateDao.delete(recordID);
			if (effectedNum <= 0) {
				throw new GZHMessageTemplateOperationException("GZHMessageTemplate单条信息删除失败");
			}
		} catch (Exception e) {
			throw new GZHMessageTemplateOperationException("Delete gZHMessageTemplate error:" + e.getMessage());
		}
		return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.SUCCESS);
	}

	@Override
	@Transactional
	public GZHMessageTemplateExecution remove(GZHMessageTemplate gZHMessageTemplate) throws GZHMessageTemplateOperationException{
		// 空值判断
		if (gZHMessageTemplate == null) {
			return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = gZHMessageTemplateDao.delete(gZHMessageTemplate.getRecordID());
			if (effectedNum <= 0) {
				throw new GZHMessageTemplateOperationException("GZHMessageTemplate单条信息删除失败");
			}
		} catch (Exception e) {
			throw new GZHMessageTemplateOperationException("Delete gZHMessageTemplate error:" + e.getMessage());
		}
		return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.SUCCESS, gZHMessageTemplate);
	}

	@Override
	@Transactional
	public GZHMessageTemplateExecution removeBatchByID(List<Long> recordIDList) throws GZHMessageTemplateOperationException{
		// 空值判断
		if (recordIDList == null || recordIDList.size() < 1) {
			return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = gZHMessageTemplateDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new GZHMessageTemplateOperationException("GZHMessageTemplate多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new GZHMessageTemplateOperationException("Batch Delete gZHMessageTemplate error:" + e.getMessage());
		}
		GZHMessageTemplateExecution exe = new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

	@Override
	@Transactional
	public GZHMessageTemplateExecution removeBatch(List<GZHMessageTemplate> gZHMessageTemplateList) throws GZHMessageTemplateOperationException{
		// 空值判断
		if (gZHMessageTemplateList == null) {
			return new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.NULL);
		} 
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<gZHMessageTemplateList.size(); i++) {
			recordIDList.add(gZHMessageTemplateList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = gZHMessageTemplateDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new GZHMessageTemplateOperationException("GZHMessageTemplate多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new GZHMessageTemplateOperationException("Batch Delete gZHMessageTemplate error:" + e.getMessage());
		}
		GZHMessageTemplateExecution exe = new GZHMessageTemplateExecution(GZHMessageTemplateOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

}
